package com.spic.business.accident.interevents.eviewreport.controller;

import com.spic.business.accident.interevents.eviewreport.domain.InternalEventReviewReport;
import com.spic.business.accident.interevents.eviewreport.domain.ReviewDto;
import com.spic.business.accident.interevents.eviewreport.service.IInternalEventReviewReportService;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 内部事件复查报告Controller
 *
 * @author 任翔
 * @date 2021-12-01
 */
@RestController
@RequestMapping("/accident/eviewReport")
public class InternalEventReviewReportController extends BaseController
{
    @Autowired
    private IInternalEventReviewReportService internalEventReviewReportService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询内部事件复查报告列表
     */
    @PreAuthorize(hasPermi = "business:eviewReport:list")
    @GetMapping("/list")
    public TableDataInfo list(InternalEventReviewReport internalEventReviewReport)
    {
        startPage();
        List<InternalEventReviewReport> list = internalEventReviewReportService.selectInternalEventReviewReportList(internalEventReviewReport);
        return getDataTable(list);
    }

    @GetMapping("/reviewList")
    public TableDataInfo reviewList(@RequestParam Map map)
    {
        //获取部门及子部门
        if (null !=  map.get("reviewDept")){
            List<Long> reviewDepts = remoteDeptService.getDeptAndChildren(Long.parseLong(map.get("reviewDept").toString())).getData();
            map.put("reviewDepts",reviewDepts);
        }
        startPage();
        List<ReviewDto> list = internalEventReviewReportService.reviewList(map);
        return getDataTable(list);
    }

    /**
     * 导出内部事件复查报告列表
     */
    @PreAuthorize(hasPermi = "business:eviewReport:export")
    @Log(title = "内部事件复查报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InternalEventReviewReport internalEventReviewReport) throws IOException
    {
        List<InternalEventReviewReport> list = internalEventReviewReportService.selectInternalEventReviewReportList(internalEventReviewReport);
        ExcelUtil<InternalEventReviewReport> util = new ExcelUtil<InternalEventReviewReport>(InternalEventReviewReport.class);
        util.exportExcel(response, list, "内部事件复查报告数据");
    }

    /**
     * 获取内部事件复查报告详细信息
     */
    @PreAuthorize(hasPermi = "business:eviewReport:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(internalEventReviewReportService.selectInternalEventReviewReportById(id));
    }

    /**
     * 新增内部事件复查报告
     */
    @PreAuthorize(hasPermi = "business:eviewReport:add")
    @Log(title = "内部事件复查报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InternalEventReviewReport internalEventReviewReport)
    {
        Long eventId = internalEventReviewReport.getEventId();
        if(eventId !=null){
            InternalEventReviewReport in = new InternalEventReviewReport();
            in.setEventId(internalEventReviewReport.getEventId());
            List<InternalEventReviewReport> inList = internalEventReviewReportService.selectInternalEventReviewReportList(in);
            if(inList!=null && inList.size()>0){
                return AjaxResult.error("事故分析报告已存在复查记录");
            } else{
                return toAjax(internalEventReviewReportService.insertInternalEventReviewReport(internalEventReviewReport));
            }
        }
        return AjaxResult.error("事故分析报告异常！");
    }

    /**
     * 修改内部事件复查报告
     */
    @PreAuthorize(hasPermi = "business:eviewReport:edit")
    @Log(title = "内部事件复查报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InternalEventReviewReport internalEventReviewReport)
    {
        return toAjax(internalEventReviewReportService.updateInternalEventReviewReport(internalEventReviewReport));
    }

    /**
     * 删除内部事件复查报告
     */
    @PreAuthorize(hasPermi = "business:eviewReport:remove")
    @Log(title = "内部事件复查报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(internalEventReviewReportService.deleteInternalEventReviewReportByIds(ids));
    }
}
